package com.coollang.smartbra.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.beans.ActionData;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.UIUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Timmy
 * @Data: 2017/5/17
 * @Fuction: 平板支撑详情数据
 */

public class PbzcFragmentAdapter extends BaseAdapter {

    private Context mContext;
    private List<Integer> mList;

    public PbzcFragmentAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
        mActionDatas = new ArrayList<>();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pbzcfragment_listview, parent, false);

            holder.tv_currtime = (TextView) convertView.findViewById(R.id.tv_currtime);
            holder.tv_position = (TextView) convertView.findViewById(R.id.tv_position);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_error_info = (TextView) convertView.findViewById(R.id.tv_error_info);
            holder.item_div = convertView.findViewById(R.id.item_div);

            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        int time = mList.get(position);
        int count = getCount();

        ActionData data = mActionDatas.get(position);
        // String errorInfo = data.getErrorInfo();
        //holder.tv_error_info.setText(errorInfo);


        // =====================================================


        holder.tv_currtime.setVisibility(View.VISIBLE);
        if (position == 0) {
            // 说明是第一条
            if (data.getErrorFlag() == 1) {
                setTextBlack(holder);
                holder.tv_error_info.setVisibility(View.GONE);
                holder.tv_currtime.setText("");

            } else {
                setTextRed(holder);
                holder.tv_currtime.setVisibility(View.GONE);
                holder.tv_error_info.setVisibility(View.VISIBLE);
                holder.tv_error_info.setText(data.getErrorInfo());
            }
        } else {
            //不是第一条
            if (data.getErrorFlag() == 1) {
                setTextBlack(holder);
                holder.tv_error_info.setVisibility(View.GONE);
                if (position == count - 1) {
                    holder.tv_currtime.setText("+ " + TimeUtils.getFormatmillisecond(mList.get(count - 1)));
                } else {
                    holder.tv_currtime.setText("+ " + TimeUtils.getFormatmillisecond(time - mList.get(position + 1)));
                }

            } else {
                setTextRed(holder);
                holder.tv_error_info.setVisibility(View.VISIBLE);
                holder.tv_error_info.setText(data.getErrorInfo());
                holder.tv_currtime.setText(TimeUtils.getFormatmillisecond(time - mList.get(position + 1)));
            }
        }

        // =====================================================

        // 设置引导数字
        if (position < 10) {
            holder.tv_position.setText("0" + (count - position));
        } else {
            holder.tv_position.setText((count - position) + "");
        }

        if (position == count - 1) {
            holder.item_div.setVisibility(View.INVISIBLE);
        } else {
            holder.item_div.setVisibility(View.VISIBLE);
        }
        holder.tv_time.setText(TimeUtils.getFormatmillisecond(time));

        return convertView;
    }

    private ArrayList<ActionData> mActionDatas;

    public void addData(int time, ActionData actionData) {
        mList.add(0, time);
        mActionDatas.add(0, actionData);
        notifyDataSetChanged();
    }


    public ArrayList<Integer> getData() {
        return (ArrayList<Integer>) mList;
    }

    public ArrayList<ActionData> getActionData() {
        return mActionDatas;
    }

    public void upData(int time) {
        if (mList.size() != 0) {
            mList.set(0, time);
            notifyDataSetChanged();
        }
    }

    // 黑色
    private void setTextRed(ViewHolder holder) {
        holder.tv_currtime.setTextColor(UIUtils.getColor(R.color.pbzc_listview_item));
        holder.tv_position.setTextColor(UIUtils.getColor(R.color.pbzc_listview_item));
        holder.tv_time.setTextColor(UIUtils.getColor(R.color.pbzc_listview_item));
    }

    // 设置颜色红色
    private void setTextBlack(ViewHolder holder) {
        holder.tv_currtime.setTextColor(UIUtils.getColor(R.color.black_666_60));
        holder.tv_position.setTextColor(UIUtils.getColor(R.color.black_666));
        holder.tv_time.setTextColor(UIUtils.getColor(R.color.black_666));
    }

    static class ViewHolder {
        public TextView tv_position;
        public TextView tv_currtime;
        public TextView tv_error_info;
        public TextView tv_time;
        public View item_div;
    }
}
